/*
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package cn.cloudbae.config.jobconig;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.dataflow.DataflowJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;

import cn.cloudbae.schedule.FirstDataFlowJob;


@Configuration
public class ESJobConfig {
	
	//引用zk
    @Autowired
    ZookeeperRegistryCenter zookeeperRegistryCenter;

	/**
	 * 初始化一个dataflow job bean，并注入ioc容器
	 */
    @Bean(initMethod = "init", name="firstDataFlowJob")
    public JobScheduler initFirstSimpleJob() {
    	String jobName = "firstDataFlowJob"; //job名称
    	String cron = "0/15 * * * * ?";  //15秒执行一次
    	int shardingTotalCount = 8; //分片总数
    	String shardingItemParameters="0=中国,1=韩国,2=英国,3=法国,4=美国,5=巴西,6=南非,7=埃及"; //分片参数，一定要从0开始，个数与shardingTotalCount一致
    	
    	//第一步：定义作业核心配置
        JobCoreConfiguration dataflowCoreConfig = JobCoreConfiguration.newBuilder(
        		jobName, cron, shardingTotalCount) //job的名称，定时时间，分片个数
        		.shardingItemParameters(shardingItemParameters) //分片参数
        		.description("这是我第一个DataFlow job").build();
        
        //第二步：定义dataflow类型配置
        DataflowJobConfiguration dataflowJobConfig = new DataflowJobConfiguration(
        		dataflowCoreConfig, FirstDataFlowJob.class.getCanonicalName(), true);
        
        //第三步：定义Lite作业根配置
        LiteJobConfiguration dataflowJobRootConfig = LiteJobConfiguration.newBuilder(
        		dataflowJobConfig).overwrite(true).build(); //overwrite(true)表示每次启动都覆盖zk注册中心的配置
    	 return new JobScheduler(zookeeperRegistryCenter, dataflowJobRootConfig);
    }
    
    
}
